using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Utils;
using Random = UnityEngine.Random;

public class WarlockBase : Monster
{
    public float attackRange=100f;
    public float trackingRange=100f;
    public float trackingCD;
    public float strollCD;

    private float trackingtiming;
    private float strolltiming;
    private float attacktiming;

    private bool seeTarget;

    public Weapon weapon;

    private GameObject manaCube, goldCoin;

    void Awake()
    {
        roomCtr = transform.parent.parent.GetComponent<RoomController>();
        weapon = GetComponentInChildren<Weapon>();

        graveObj = UITool.GetChildGameObject("Grave", transform.parent.parent.gameObject);
        manaCube = UITool.GetChildGameObject("ManaCube", gameObject);
        goldCoin = UITool.GetChildGameObject("GoldCoin", gameObject);

        animator = GetComponent<Animator>();
        collider2D = GetComponent<BoxCollider2D>();
        spriteRenderer = GetComponent<SpriteRenderer>();
    }

    private void Start()
    {
        // targetPosition = GameController.Instance().playerCtr.Player.transform.parent; //加载模式
        // print(targetPosition.name);
        if (targetPosition == null)
        {
            targetPosition = GameController.Instance().playerCtr.transform;
            // targetPosition = GameObject.Find("Player").transform;
        }

        ai = GetComponent<AstarAI>();
        layerMask = ~LayerMask.GetMask("Enemy") & ~LayerMask.GetMask("Room");
    }

    private void Update()
    {
        switch (monsterState)
        {
            case MonsterState.Idle:
                animator.SetBool("inRun", false);
                Idle();
                break;
            case MonsterState.Tracking:
                animator.SetBool("inRun", true);
                Tracking();
                break;
            case MonsterState.Stroll:
                animator.SetBool("inRun", true);
                Stroll();
                break;
            case MonsterState.Attack:
                animator.SetBool("inRun", false);
                Attack();
                break;
            case MonsterState.Die:
                animator.SetBool("isDie", true);
                Die();
                break;
            default:
                break;
        }
    }

    private void UpdateLookAt(Vector2 targetPos)
    {
        if (transform.position.x > targetPos.x)
        {
            spriteRenderer.flipX = true;
        }
        else if (transform.position.x < targetPos.x)
        {
            spriteRenderer.flipX = false;
        }
        // weapon.UpdateLookAt(targetPos);
    }

    public override void Idle()
    {
        // Debug.Log("MonsterState: Idle");

        if (roomCtr.inFightState)
        {
            monsterState = MonsterState.Stroll;
            animator.SetBool(inRun, true);
        }
    }

    public override void Stroll()
    {
        Debug.Log("MonsterState: Stroll");

        RaycastDetection();
        if (seeTarget)
        {
            monsterState = MonsterState.Tracking;
            // Debug.Log("Turn to Tracking");
        }

        UpdateLookAt(ai.nextTargetPosition);

        if (Time.time - strolltiming >= strollCD)
        {
            strolltiming = Time.time;
            ai.RandomPath();
        }

        ai.NextTarget();
    }

    public override void Tracking()
    {
        Debug.Log("MonsterState: Tracking");

        if (Time.time - trackingtiming >= trackingCD)
        {
            trackingtiming = Time.time;
            ai.UpdatePath(targetPosition.position);
        }

        ai.NextTarget();
        if (Vector2.Distance(transform.position, targetPosition.position) <= attackRange)
        {
            monsterState = MonsterState.Attack;
            // Debug.Log("Turn to Attack");

            animator.SetBool(inRun, false);
        }

        RaycastDetection();
        if (!seeTarget)
        {
            monsterState = MonsterState.Stroll;
            // Debug.Log("Turn to Stroll");
        }

        UpdateLookAt(ai.nextTargetPosition);
    }

    public override void Attack()
    {
        Debug.Log("MonsterState: Attack");

        if (Vector2.Distance(transform.position, targetPosition.position) > attackRange)
        {
            monsterState = MonsterState.Tracking;
            // Debug.Log("Turn to Tracking");
        }

        UpdateLookAt(targetPosition.position);
        if (Time.time - attacktiming >= attackCD)
        {
            attacktiming = Time.time;
            weapon.AttackButtonPress();
        }
    }

    public override void Die()
    {
        ai.NextTarget();
    }

    public override void BeAttacked()
    {
        if (hp <= 0)
        {
            monsterState = MonsterState.Die;
            GameController.Instance().archiveCtr.TempLevelData.killed++;
            animator.SetBool(isDie, true);
            collider2D.enabled = false;
            if (GameController.Instance().archiveCtr.TempLevelData.gameType  == GameType.Dungeon)
            {
                transform.SetParent(graveObj.transform);
            }
            DropActive();

            weapon.gameObject.SetActive(false);
            ai.UpdatePath((transform.position - targetPosition.position).normalized * 2 + transform.position);
            if (isBoss)
            {
                GameController.Instance().panelCtr.RemoveBossPanel();
            }
        }
        else
        {
            animator.Play("BeAttacked");
        }
    }

    private void RaycastDetection()
    {
        hit = Physics2D.Raycast(transform.position + Vector3.up,
            (targetPosition.position - (transform.position + Vector3.up)).normalized,
            trackingRange, layerMask);
        if (hit.transform != null && hit.transform == targetPosition)
        {
            seeTarget = true;
            Debug.DrawLine(transform.position + Vector3.up, hit.transform.position, Color.red);
        }
        else
        {
            seeTarget = false;
        }
    }

    private void DropActive()
    {
        manaCube.SetActive(true);
        int i = Random.Range(0, 3);
        if (i < 2) return;
        goldCoin.SetActive(true);
        goldCoin.GetComponent<GoldCoin>().value = (int)Random.Range(3, 6);
    }
}